package com.ht.service.impl;

import com.ht.dto.Article;
import com.ht.dto.User;
import com.ht.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(User user, Article article) {
        article.setUserId(user.getId());
        article.setUserName(user.getUsername());
        article.setArticleStatus("0");
        mongoTemplate.save(article);
    }

    @Override
    public List<Article> selectArticle(User user) {
        Query query = new Query(Criteria.where("userId").is(user.getId()));
        List<Article> users = mongoTemplate.find(query, Article.class);
        return users;
    }

}
